LEADTOOLS Image File Support (Leadtools.Codecs assembly)

StartCompress(Int32,Int32,Int32,RasterByteOrder,RasterViewPerspective,Int32,Byte[],Int32,Int32,CodecsCompression,CodecsCompressDataCallback) Method

Show in webframe
Example 







The image being compressed width in pixels.
The image being compressed height in pixels.
The image being compressed bits/pixel value.
The image being compressed byte order.
The image being compressed view perspective value.
Size in bytes of the input image data.
Array of bytes which will be filled with the compressed data.
Index into outputData where the compressed data should be inserted.
Size of outputData in bytes.

Type of compression to use. Valid values are:

Value Meaning
CodecsCompression.Cmp LEAD CMP compression format
CodecsCompression.Jpeg444 JPEG File Interchange Format using YUV 4:4:4 color spacing
CodecsCompression.Jpeg422 JPEG File Interchange Format using YUV 4:2:2 color spacing
CodecsCompression.Jpeg411 JPEG File Interchange Format using YUV 4:1:1 color spacing
CodecsCompression.TifJpeg444 JPEG JTIF using YUV 4:4:4 color spacing
CodecsCompression.TifJpeg422 JPEG JTIF using YUV 4:2:2 color spacing
CodecsCompression.TifJpeg411 JPEG JTIF using YUV 4:1:1 color spacing
CodecsCompression.Lead0 LEAD 1 bit, lossless compression
CodecsCompression.Lead1 LEAD 1 bit, excellent compression
CodecsCompression.TiffCcitt TIFF CCITT
CodecsCompression.TiffCcittG3Fax1Dim CCITT Group 3 one dimensional
CodecsCompression.TiffCcittG3Fax2Dim CCITT Group 3 two dimensional
CodecsCompression.TiffCcittG4Fax CCITT Group 4 two dimensional
The callback method responsible for writing or handling the compressed data.
Initializes the buffered compression engine.
Syntax
'Declaration
 
Public Overloads Sub StartCompress( _
   ByVal width As Integer, _
   ByVal height As Integer, _
   ByVal bitsPerPixel As Integer, _
   ByVal order As RasterByteOrder, _
   ByVal viewPerspective As RasterViewPerspective, _
   ByVal inputDataLength As Integer, _
   ByVal outputData() As Byte, _
   ByVal outputDataIndex As Integer, _
   ByVal outputDataLength As Integer, _
   ByVal compression As CodecsCompression, _
   ByVal callback As CodecsCompressDataCallback _
) 
'Usage
 
Dim instance As RasterCodecs
Dim width As Integer
Dim height As Integer
Dim bitsPerPixel As Integer
Dim order As RasterByteOrder
Dim viewPerspective As RasterViewPerspective
Dim inputDataLength As Integer
Dim outputData() As Byte
Dim outputDataIndex As Integer
Dim outputDataLength As Integer
Dim compression As CodecsCompression
Dim callback As CodecsCompressDataCallback
 
instance.StartCompress(width, height, bitsPerPixel, order, viewPerspective, inputDataLength, outputData, outputDataIndex, outputDataLength, compression, callback)

            
public void startCompress(int width, int height, int bitsPerPixel, RasterByteOrder order, RasterViewPerspective viewPerspective, int inputDataLength, byte[] outputData, int outputDataIndex, int outputDataLength, CodecsCompression compression, CodecsCompressDataListener callback)
 function Leadtools.Codecs.RasterCodecs.StartCompress(Int32,Int32,Int32,RasterByteOrder,RasterViewPerspective,Int32,Byte[],Int32,Int32,CodecsCompression,CodecsCompressDataCallback)( 
   width ,
   height ,
   bitsPerPixel ,
   order ,
   viewPerspective ,
   inputDataLength ,
   outputData ,
   outputDataIndex ,
   outputDataLength ,
   compression ,
   callback 
)

Parameters

width
The image being compressed width in pixels.
height
The image being compressed height in pixels.
bitsPerPixel
The image being compressed bits/pixel value.
order
The image being compressed byte order.
viewPerspective
The image being compressed view perspective value.
inputDataLength
Size in bytes of the input image data.
outputData
Array of bytes which will be filled with the compressed data.
outputDataIndex
Index into outputData where the compressed data should be inserted.
outputDataLength
Size of outputData in bytes.
compression

Type of compression to use. Valid values are:

Value Meaning
CodecsCompression.Cmp LEAD CMP compression format
CodecsCompression.Jpeg444 JPEG File Interchange Format using YUV 4:4:4 color spacing
CodecsCompression.Jpeg422 JPEG File Interchange Format using YUV 4:2:2 color spacing
CodecsCompression.Jpeg411 JPEG File Interchange Format using YUV 4:1:1 color spacing
CodecsCompression.TifJpeg444 JPEG JTIF using YUV 4:4:4 color spacing
CodecsCompression.TifJpeg422 JPEG JTIF using YUV 4:2:2 color spacing
CodecsCompression.TifJpeg411 JPEG JTIF using YUV 4:1:1 color spacing
CodecsCompression.Lead0 LEAD 1 bit, lossless compression
CodecsCompression.Lead1 LEAD 1 bit, excellent compression
CodecsCompression.TiffCcitt TIFF CCITT
CodecsCompression.TiffCcittG3Fax1Dim CCITT Group 3 one dimensional
CodecsCompression.TiffCcittG3Fax2Dim CCITT Group 3 two dimensional
CodecsCompression.TiffCcittG4Fax CCITT Group 4 two dimensional
callback
The callback method responsible for writing or handling the compressed data.
Remarks

This method initializes the buffered compression engine. The compression is then carried out using the Compress(Byte[],Int32) method. It is ended by the StopCompress method.

If order is set to to RasterByteOrder.Bgr and viewPerspective is RasterViewPerspective.TopLeft then the data that you put into the input buffer must be RasterByteOrder.Bgr and loaded from top left.

The compression process starts after the first call to Compress(Byte[],Int32). The callback is called when the output buffer is filled with compressed data or after completing the compression process. callback is responsible for emptying the output buffer - storing it, sending it, or doing other processing.

The following is a flow chart that shows the relationship of these methods:

Call StopCompress to end the compression process started by a call to StartCompress(Int32,Int32,Int32,RasterByteOrder,RasterViewPerspective,Int32,Byte[],Int32,Int32,CodecsCompression,CodecsCompressDataCallback).

The quality factor of the compressed data is obtained as follows:

Format Quality factor used
CodecsCompression.Cmp CodecsJpegSaveOptions.QualityFactor and CodecsJpegSaveOptions.CmpQualityFactorPredefined
CodecsCompression.Jpeg444 CodecsJpegSaveOptions.QualityFactor
CodecsCompression.Jpeg422 CodecsJpegSaveOptions.QualityFactor
CodecsCompression.Jpeg411 CodecsJpegSaveOptions.QualityFactor
CodecsCompression.TifJpeg444 CodecsJpegSaveOptions.QualityFactor
CodecsCompression.TifJpeg422 CodecsJpegSaveOptions.QualityFactor
CodecsCompression.TifJpeg411 CodecsJpegSaveOptions.QualityFactor
CodecsCompression.Lead0 Not used
CodecsCompression.Lead1 Not used
CodecsCompression.TiffCcitt Not used
CodecsCompression.TiffCcittG3Fax1Dim Not used
CodecsCompression.TiffCcittG3Fax2Dim Not used
CodecsCompression.TiffCcittG4Fax Not used

This method does not support signed data images.

Example
Copy Code  
Imports Leadtools
Imports Leadtools.Codecs
Imports Leadtools.ImageProcessing
Imports Leadtools.ImageProcessing.Color
Imports Leadtools.Drawing

Public Sub CompressExample()
   Dim codecs As RasterCodecs = New RasterCodecs()

   Dim srcFileName As String = Path.Combine(LEAD_VARS.ImagesDir, "Image1.cmp")
   Dim destFileName As String = Path.Combine(LEAD_VARS.ImagesDir, "Image1_CompressData.cmp")

   ' Load the image to at 24-bits per pixel
   Dim image As RasterImage = codecs.Load(srcFileName, 24, CodecsLoadByteOrder.Bgr, 1, 1)
   If image.ViewPerspective <> RasterViewPerspective.TopLeft Then
      image.ChangeViewPerspective(RasterViewPerspective.TopLeft)
   End If

   ' Create the output file
   _compressStream = File.Create(destFileName)

   ' Calculate the bytes per line in the input buffer, without padding 
   Dim lineBytes As Integer = image.Width * 3

   ' Allocate a buffer for the incoming uncompressed data. Note that we
   '  are compressing 16 lines at a time. You should always use multiples of 16
   Dim inBuffer As Byte() = New Byte(16 * lineBytes - 1) {}

   ' Allocate an output buffer. This is where the compressed data will
   ' go.  Note that this allocates 1024-byte packets. 
   Dim outBuffer As Byte() = New Byte(1023) {}

   ' Lock down the image
   image.Access()

   ' Initialize the compression engine
   codecs.Options.Jpeg.Save.CmpQualityFactorPredefined = CodecsCmpQualityFactorPredefined.QualityAndSize
   codecs.StartCompress(image.Width, image.Height, image.BitsPerPixel, image.Order, image.ViewPerspective, 16 * lineBytes, outBuffer, 0, outBuffer.Length, CodecsCompression.Cmp, AddressOf MyCodecsCompressDataCallback)

   ' Compress the data
   Dim i As Integer = 0
   Do While i < image.Height ' i is incremented at the end
      ' Compression of the 16-line chunk starts here
      Dim j As Integer = 0
      Dim inBufferIndex As Integer = 0
      Do While (i + j) < image.Height AndAlso j < 16
         ' Get one line at time
         image.GetRow(i + j, inBuffer, inBufferIndex, lineBytes)

         ' Move to next line
         inBufferIndex += lineBytes

         j += 1
      Loop

      ' This is the main function that will do the actual Compression
      codecs.Compress(inBuffer, 0)
      i += 16
   Loop

   ' Reset the compression engine 
   codecs.StopCompress()

   ' Release the image and close the file
   image.Release()
   _compressStream.Close()

   ' Clean up
   codecs.Dispose()
End Sub

Private _compressStream As FileStream
Private _compressBuffer As Byte()

Private Function MyCodecsCompressDataCallback(ByVal width As Integer, ByVal height As Integer, ByVal bitsPerPixel As Integer, ByVal order As RasterByteOrder, ByVal viewPerspective As RasterViewPerspective, ByVal buffer As RasterNativeBuffer) As Boolean
   ' Write data to the file
   If _compressBuffer Is Nothing OrElse _compressBuffer.Length < buffer.Length Then
      _compressBuffer = New Byte(CType(buffer.Length, Integer) - 1) {}
   End If

   Marshal.Copy(buffer.Data, _compressBuffer, 0, CType(buffer.Length, Integer))

   _compressStream.Write(_compressBuffer, 0, CType(buffer.Length, Integer))

   Return True
End Function

Public NotInheritable Class LEAD_VARS
Public Const ImagesDir As String = "C:\Users\Public\Documents\LEADTOOLS Images"
End Class
using Leadtools;
using Leadtools.Codecs;
using Leadtools.ImageProcessing;
using Leadtools.ImageProcessing.Color;

public void CompressExample()
{
   RasterCodecs codecs = new RasterCodecs();

   string srcFileName = Path.Combine(LEAD_VARS.ImagesDir, "Image1.cmp");
   string destFileName = Path.Combine(LEAD_VARS.ImagesDir, "Image1_CompressData.cmp");

   // Load the image to at 24-bits per pixel
   RasterImage image = codecs.Load(srcFileName, 24, CodecsLoadByteOrder.Bgr, 1, 1);
   if (image.ViewPerspective != RasterViewPerspective.TopLeft)
      image.ChangeViewPerspective(RasterViewPerspective.TopLeft);

   // Create the output file
   _compressStream = File.Create(destFileName);

   // Calculate the bytes per line in the input buffer, without padding 
   int lineBytes = image.Width * 3;

   // Allocate a buffer for the incoming uncompressed data. Note that we
   //  are compressing 16 lines at a time. You should always use multiples of 16
   byte[] inBuffer = new byte[16 * lineBytes];

   // Allocate an output buffer. This is where the compressed data will
   // go.  Note that this allocates 1024-byte packets. 
   byte[] outBuffer = new byte[1024];

   // Lock down the image
   image.Access();

   // Initialize the compression engine
   codecs.Options.Jpeg.Save.CmpQualityFactorPredefined = CodecsCmpQualityFactorPredefined.QualityAndSize;
   codecs.StartCompress(
      image.Width,
      image.Height,
      image.BitsPerPixel,
      image.Order,
      image.ViewPerspective,
      16 * lineBytes,
      outBuffer,
      0,
      outBuffer.Length,
      CodecsCompression.Cmp,
      MyCodecsCompressDataCallback);

   // Compress the data
   int i = 0;
   while (i < image.Height) // i is incremented at the end
   {
      // Compression of the 16-line chunk starts here
      int j = 0;
      int inBufferIndex = 0;
      while ((i + j) < image.Height && j < 16)
      {
         // Get one line at time
         image.GetRow(i + j, inBuffer, inBufferIndex, lineBytes);

         // Move to next line
         inBufferIndex += lineBytes;

         j++;
      }

      // This is the main function that will do the actual Compression
      codecs.Compress(inBuffer, 0);
      i += 16;
   }

   // Reset the compression engine 
   codecs.StopCompress();

   // Release the image and close the file
   image.Release();
   _compressStream.Close();

   // Clean up
   codecs.Dispose();
}

FileStream _compressStream;
byte[] _compressBuffer;

bool MyCodecsCompressDataCallback(int width, int height, int bitsPerPixel, RasterByteOrder order, RasterViewPerspective viewPerspective, RasterNativeBuffer buffer)
{
   // Write data to the file
   if (_compressBuffer == null || _compressBuffer.Length < buffer.Length)
      _compressBuffer = new byte[(int)buffer.Length];

   Marshal.Copy(buffer.Data, _compressBuffer, 0, (int)buffer.Length);

   _compressStream.Write(_compressBuffer, 0, (int)buffer.Length);

   return true;
}

static class LEAD_VARS
{
public const string ImagesDir = @"C:\Users\Public\Documents\LEADTOOLS Images";
}
using Leadtools;
using Leadtools.Codecs;
using Leadtools.ImageProcessing;
using Leadtools.ImageProcessing.Color;

      
public async Task CompressExample()
{
   Debug.WriteLine("RasterCodecs.Compress - 0");
   RasterCodecs codecs = new RasterCodecs();
   Debug.WriteLine("RasterCodecs.Compress - 1");

   string srcFileName = @"Assets\Image1.cmp";
   string destFileName = "Image1_CompressData.cmp";

   // Load the image to at 24-bits per pixel
   IStorageFile loadFile = await Tools.AppInstallFolder.GetFileAsync(srcFileName);
   RasterImage image = await codecs.LoadAsync(LeadStreamFactory.Create(loadFile), 24, CodecsLoadByteOrder.Bgr, 1, 1);
   if (image.ViewPerspective != RasterViewPerspective.TopLeft)
      image.ChangeViewPerspective(RasterViewPerspective.TopLeft);

   // Create the output file
   IStorageFile saveFile = await Tools.AppLocalFolder.CreateFileAsync(destFileName);
   _compressStream = await saveFile.OpenStreamForWriteAsync();

   // Calculate the bytes per line in the input buffer, without padding 
   int lineBytes = image.Width * 3;

   // Allocate a buffer for the incoming uncompressed data. Note that we
   //  are compressing 16 lines at a time. You should always use multiples of 16
   byte[] inBuffer = new byte[16 * lineBytes];

   // Lock down the image
   image.AccessData();

   // Let the function allocate a buffer of 1024 bytes for storing the compressed data
   // This buffer will be passed to our callback

   // Initialize the compression engine
   codecs.Options.Jpeg.Save.CmpQualityFactorPredefined = CodecsCmpQualityFactorPredefined.QualityAndSize;
   codecs.StartCompress(
      image.Width,
      image.Height,
      image.BitsPerPixel,
      image.Order,
      image.ViewPerspective,
      16 * lineBytes,
      1024,
      CodecsCompression.Cmp,
      MyCodecsCompressDataCallback);

   // Compress the data
   int i = 0;
   while (i < image.Height) // i is incremented at the end
   {
      // Compression of the 16-line chunk starts here
      int j = 0;
      int inBufferIndex = 0;
      while ((i + j) < image.Height && j < 16)
      {
         byte[] lineBuffer = new byte[lineBytes];

         // Get one line at time. I cannot read into inBuffer directly because buffer passed to GetRow is write-only
         // and its contents is destroyed when you call GetRow. 
         image.GetRow(i + j, lineBuffer, 0, lineBytes);

         // copy the one-row lineBuffer to the 16-row lineBuffer
         Array.Copy(lineBuffer, 0, inBuffer, j * lineBytes, lineBytes);

         // Move to next line
         inBufferIndex += lineBytes;

         j++;
      }

      // This is the main function that will do the actual Compression
      codecs.Compress(inBuffer, 0);
      i += 16;
   }

   // Reset the compression engine 
   codecs.StopCompress();

   // Release the image and close the file
   image.ReleaseData();
   _compressStream.Dispose();

   // Clean up
   codecs.Dispose();
}

Stream _compressStream;
byte[] _compressBuffer;

bool MyCodecsCompressDataCallback(int width, int height, int bitsPerPixel, RasterByteOrder order, RasterViewPerspective viewPerspective, RasterNativeBuffer buffer)
{
   int bufferLength = (int)buffer.Length;
   // Write data to the file
   if (_compressBuffer == null || _compressBuffer.Length < bufferLength)
      _compressBuffer = new byte[bufferLength];

   buffer.GetData(0, _compressBuffer, 0, bufferLength);
   _compressStream.Write(_compressBuffer, 0, bufferLength);

   return true;
}
Requirements

Target Platforms

See Also

Reference

RasterCodecs Class
RasterCodecs Members
Overload List
Compressing and Decompressing Buffers
Speeding Up 1-Bit Documents

 

 


Products | Support | Contact Us | Copyright Notices
© 2006-2014 All Rights Reserved. LEAD Technologies, Inc.